import { createRouter, createWebHashHistory } from 'vue-router'

import { getToken } from '@/composables/auth.js'
import  {toast} from  "@/composables/util.js"
import Home from '@/views/Home.vue'
import Login from "@/views/Login.vue";
import NotFound from '@/views/404.vue'
import Backend1 from '@/views/Backend1.vue'
import Backend2 from "@/views/Backend2.vue";
import User from "@/views/User.vue";
import User2 from "@/views/User2.vue"
import About from "@/views/About.vue";
import Dash from "@/views/Dash.vue";

import {Compass, Connection, Document, Guide, House, Location, Menu, Position, Setting} from "@element-plus/icons-vue";


export const nav_routes = [
    {
        path: '/home',
        name: 'Home',
        redirect:'/dash',
        component:Home,
        children: [ {
            path: '/dash',
            name: 'dash',
            meta: {title: '首页', icon: House},
            component:Dash},
            {
            path: '/backend',
            name: 'Backend',
            meta: {title: '监测后台', icon: Compass,},children:[
                {path: 'backend1',
                name: 'backend1',
                meta: {title: '数据库', icon:Connection},
                component: Backend1,},
                {path: 'backend2',
                name: 'backend2',
                meta: {title: 'MQTT测试', icon: Location},
                component: Backend2,}
            ]}, {
            path: '/user',
            name: 'user',
            meta: {title: '用户管理', icon: 'User'},children: [{
                path: 'user1',
                name: 'user1',
                meta: {title: '用户列表', icon: Position},
                component: User}, {
                path: 'user2',
                name: 'user2',
                meta: {title: '后端接口测试', icon: Guide},
                component: User2
                }]
            }, {
            path: '/about',
            name: 'about',
            meta: {title: '关于', icon: Document},
            component: About
            },]
        }]
export const  static_routes=  [{
        path: '/login',
        name: 'login',
        component: Login
        }, {
        path: '/',
        redirect:'/login'
        },{
        path: '/:pathMatch(.*)*',
        name: 'NotFound',
        component: NotFound
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes: [...nav_routes, ...static_routes],

})
router.beforeEach(async (to,from,next)=>{
    const token=getToken()
    if (!token &&to.path!=="/login"){
        toast("请先登录","error")
        return next({path:"/login"})
    }
    // if (token &&to.path==="/login"){
    //     toast("已登录，请勿重复登录","error")
    //     return next({path:from.path ? from.path:"/home"})
    // }
    next()
})

export default router